{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Age</th>\n",
       "      <th>Height</th>\n",
       "      <th>Team</th>\n",
       "      <th>Year</th>\n",
       "      <th>Season</th>\n",
       "      <th>Sport</th>\n",
       "      <th>Medal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>China</td>\n",
       "      <td>1992</td>\n",
       "      <td>Summer</td>\n",
       "      <td>Basketball</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>China</td>\n",
       "      <td>2012</td>\n",
       "      <td>Summer</td>\n",
       "      <td>Judo</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>31.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>United States</td>\n",
       "      <td>1992</td>\n",
       "      <td>Winter</td>\n",
       "      <td>Cross Country Skiing</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>31.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>United States</td>\n",
       "      <td>1992</td>\n",
       "      <td>Winter</td>\n",
       "      <td>Cross Country Skiing</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>31.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>United States</td>\n",
       "      <td>1992</td>\n",
       "      <td>Winter</td>\n",
       "      <td>Cross Country Skiing</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Age  Height           Team  Year  Season                 Sport Medal\n",
       "0   24.0   180.0          China  1992  Summer            Basketball   NaN\n",
       "1   23.0   170.0          China  2012  Summer                  Judo   NaN\n",
       "10  31.0   188.0  United States  1992  Winter  Cross Country Skiing   NaN\n",
       "11  31.0   188.0  United States  1992  Winter  Cross Country Skiing   NaN\n",
       "12  31.0   188.0  United States  1992  Winter  Cross Country Skiing   NaN"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/olympic_athlete_events.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Age', 'Height', 'Team', 'Year', 'Season', 'Sport', 'Medal'])\n",
    "\n",
    "df = df.loc[df['Team'].isin(['Great Britain', 'France', 'United States', 'Switzerland', 'China', 'India'])]\n",
    "df = df.loc[df['Year'] >= 1980]\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Create a pivot table that shows the number of medals that each team won per year, with the index including not just the year but also the season in which the games took place."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>China</th>\n",
       "      <th>France</th>\n",
       "      <th>Great Britain</th>\n",
       "      <th>India</th>\n",
       "      <th>Switzerland</th>\n",
       "      <th>United States</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th>Season</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1980</th>\n",
       "      <th>Summer</th>\n",
       "      <td>NaN</td>\n",
       "      <td>29.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1984</th>\n",
       "      <th>Summer</th>\n",
       "      <td>74.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.0</td>\n",
       "      <td>352.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1988</th>\n",
       "      <th>Summer</th>\n",
       "      <td>50.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>207.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1992</th>\n",
       "      <th>Summer</th>\n",
       "      <td>70.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>222.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>3.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1994</th>\n",
       "      <th>Winter</th>\n",
       "      <td>3.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <th>Summer</th>\n",
       "      <td>94.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>255.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <th>Winter</th>\n",
       "      <td>14.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <th>Summer</th>\n",
       "      <td>65.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>240.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <th>Winter</th>\n",
       "      <td>14.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "      <td>70.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <th>Summer</th>\n",
       "      <td>82.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>259.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <th>Winter</th>\n",
       "      <td>9.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15.0</td>\n",
       "      <td>48.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <th>Summer</th>\n",
       "      <td>170.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>309.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <th>Winter</th>\n",
       "      <td>15.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>89.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <th>Summer</th>\n",
       "      <td>117.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>122.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>238.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <th>Winter</th>\n",
       "      <td>12.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28.0</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <th>Summer</th>\n",
       "      <td>109.0</td>\n",
       "      <td>96.0</td>\n",
       "      <td>145.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>256.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Team         China  France  Great Britain  India  Switzerland  United States\n",
       "Year Season                                                                 \n",
       "1980 Summer    NaN    29.0           47.0   16.0          2.0            NaN\n",
       "     Winter    NaN     1.0            1.0    NaN          3.0           30.0\n",
       "1984 Summer   74.0    67.0           71.0    NaN         13.0          352.0\n",
       "     Winter    NaN     3.0            NaN    NaN          4.0            7.0\n",
       "1988 Summer   50.0    29.0           54.0    NaN          8.0          207.0\n",
       "     Winter    NaN     2.0            NaN    NaN         16.0            5.0\n",
       "1992 Summer   70.0    55.0           50.0    NaN          1.0          222.0\n",
       "     Winter    3.0    10.0            NaN    NaN          1.0           14.0\n",
       "1994 Winter    3.0    11.0            3.0    NaN          8.0           19.0\n",
       "1996 Summer   94.0    49.0           26.0    1.0         11.0          255.0\n",
       "1998 Winter   14.0    13.0            4.0    NaN         10.0           30.0\n",
       "2000 Summer   65.0    64.0           52.0    1.0         14.0          240.0\n",
       "2002 Winter   14.0    13.0            6.0    NaN         20.0           70.0\n",
       "2004 Summer   82.0    53.0           55.0    1.0          5.0          259.0\n",
       "2006 Winter    9.0    15.0            1.0    NaN         15.0           48.0\n",
       "2008 Summer  170.0    77.0           81.0    3.0         11.0          309.0\n",
       "2010 Winter   15.0    14.0            1.0    NaN         12.0           89.0\n",
       "2012 Summer  117.0    78.0          122.0    6.0          4.0          238.0\n",
       "2014 Winter   12.0    18.0           10.0    NaN         28.0           52.0\n",
       "2016 Summer  109.0    96.0          145.0    2.0         11.0          256.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df.dropna(subset='Medal'), \n",
    "               index=['Year', 'Season'], \n",
    "               columns='Team', \n",
    "               values='Medal', \n",
    "               aggfunc='size')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "Create a pivot table that shows both the average age the the average height per year, per team."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">Age</th>\n",
       "      <th colspan=\"6\" halign=\"left\">Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Team</th>\n",
       "      <th>China</th>\n",
       "      <th>France</th>\n",
       "      <th>Great Britain</th>\n",
       "      <th>India</th>\n",
       "      <th>Switzerland</th>\n",
       "      <th>United States</th>\n",
       "      <th>China</th>\n",
       "      <th>France</th>\n",
       "      <th>Great Britain</th>\n",
       "      <th>India</th>\n",
       "      <th>Switzerland</th>\n",
       "      <th>United States</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1980</th>\n",
       "      <td>32.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>196.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1984</th>\n",
       "      <td>30.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>216.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1988</th>\n",
       "      <td>34.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1992</th>\n",
       "      <td>35.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1994</th>\n",
       "      <td>29.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>35.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>192.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <td>39.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>201.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <td>29.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>198.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>195.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>39.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>207.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>30.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>190.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>43.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>32.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>190.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>195.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>45.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>211.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>34.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>194.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>41.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>221.0</td>\n",
       "      <td>208.0</td>\n",
       "      <td>211.0</td>\n",
       "      <td>196.0</td>\n",
       "      <td>198.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>32.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>173.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>196.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>194.0</td>\n",
       "      <td>211.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Age                                                      Height         \\\n",
       "Team China France Great Britain India Switzerland United States  China France   \n",
       "Year                                                                            \n",
       "1980  32.0   34.0          37.0  56.0        47.0          32.0  170.0  203.0   \n",
       "1984  30.0   50.0          47.0  45.0        52.0          49.0  216.0  214.0   \n",
       "1988  34.0   43.0          55.0  36.0        47.0          52.0  210.0  203.0   \n",
       "1992  35.0   47.0          48.0  38.0        53.0          54.0  215.0  203.0   \n",
       "1994  29.0   33.0          36.0   NaN        35.0          36.0  181.0  195.0   \n",
       "1996  39.0   53.0          48.0  46.0        49.0          50.0  215.0  201.0   \n",
       "1998  29.0   34.0          36.0  16.0        37.0          40.0  185.0  198.0   \n",
       "2000  39.0   46.0          52.0  38.0        53.0          46.0  226.0  218.0   \n",
       "2002  30.0   38.0          40.0  20.0        36.0          48.0  188.0  190.0   \n",
       "2004  43.0   50.0          48.0  43.0        55.0          52.0  226.0  202.0   \n",
       "2006  32.0   38.0          40.0  29.0        42.0          44.0  188.0  190.0   \n",
       "2008  45.0   51.0          53.0  42.0        45.0          58.0  226.0  203.0   \n",
       "2010  34.0   38.0          45.0  28.0        41.0          40.0  194.0  191.0   \n",
       "2012  41.0   49.0          56.0  39.0        49.0          54.0  221.0  208.0   \n",
       "2014  32.0   37.0          41.0  30.0        42.0          45.0  193.0  191.0   \n",
       "2016  38.0   53.0          60.0  43.0        50.0          52.0  218.0  215.0   \n",
       "\n",
       "                                                     \n",
       "Team Great Britain  India Switzerland United States  \n",
       "Year                                                 \n",
       "1980         205.0  196.0       197.0         193.0  \n",
       "1984         203.0  188.0       204.0         213.0  \n",
       "1988         205.0  193.0       204.0         216.0  \n",
       "1992         205.0  188.0       200.0         216.0  \n",
       "1994         185.0    NaN       192.0         193.0  \n",
       "1996         205.0  191.0       200.0         216.0  \n",
       "1998         185.0  183.0       192.0         195.0  \n",
       "2000         204.0  195.0       197.0         207.0  \n",
       "2002         193.0  183.0       192.0         193.0  \n",
       "2004         207.0  195.0       197.0         213.0  \n",
       "2006         193.0  183.0       193.0         195.0  \n",
       "2008         207.0  192.0       197.0         211.0  \n",
       "2010         193.0  183.0       203.0         193.0  \n",
       "2012         211.0  196.0       198.0         216.0  \n",
       "2014         193.0  173.0       200.0         196.0  \n",
       "2016         207.0  200.0       194.0         211.0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df, \n",
    "               index='Year', \n",
    "               columns='Team', \n",
    "               values=['Age', 'Height'], \n",
    "               aggfunc='max')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    " Create a pivot table that shows both the average age the the average height per year, per team, broken up by year and season."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">Age</th>\n",
       "      <th colspan=\"6\" halign=\"left\">Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>China</th>\n",
       "      <th>France</th>\n",
       "      <th>Great Britain</th>\n",
       "      <th>India</th>\n",
       "      <th>Switzerland</th>\n",
       "      <th>United States</th>\n",
       "      <th>China</th>\n",
       "      <th>France</th>\n",
       "      <th>Great Britain</th>\n",
       "      <th>India</th>\n",
       "      <th>Switzerland</th>\n",
       "      <th>United States</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th>Season</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1980</th>\n",
       "      <th>Summer</th>\n",
       "      <td>NaN</td>\n",
       "      <td>34.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>196.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>32.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>186.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1984</th>\n",
       "      <th>Summer</th>\n",
       "      <td>30.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>216.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>28.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>189.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>184.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1988</th>\n",
       "      <th>Summer</th>\n",
       "      <td>34.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>24.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>186.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>189.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1992</th>\n",
       "      <th>Summer</th>\n",
       "      <td>35.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Winter</th>\n",
       "      <td>29.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>194.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>192.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1994</th>\n",
       "      <th>Winter</th>\n",
       "      <td>29.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>35.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>192.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <th>Summer</th>\n",
       "      <td>39.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>201.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <th>Winter</th>\n",
       "      <td>29.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>198.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>195.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <th>Summer</th>\n",
       "      <td>39.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>204.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>207.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <th>Winter</th>\n",
       "      <td>30.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>190.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <th>Summer</th>\n",
       "      <td>43.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <th>Winter</th>\n",
       "      <td>32.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>190.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>195.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <th>Summer</th>\n",
       "      <td>45.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>211.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <th>Winter</th>\n",
       "      <td>34.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>194.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>183.0</td>\n",
       "      <td>203.0</td>\n",
       "      <td>193.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <th>Summer</th>\n",
       "      <td>41.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>221.0</td>\n",
       "      <td>208.0</td>\n",
       "      <td>211.0</td>\n",
       "      <td>196.0</td>\n",
       "      <td>198.0</td>\n",
       "      <td>216.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <th>Winter</th>\n",
       "      <td>32.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>173.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>196.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <th>Summer</th>\n",
       "      <td>38.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>194.0</td>\n",
       "      <td>211.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Age                                                      Height  \\\n",
       "Team        China France Great Britain India Switzerland United States  China   \n",
       "Year Season                                                                     \n",
       "1980 Summer   NaN   34.0          37.0  56.0        47.0           NaN    NaN   \n",
       "     Winter  32.0   29.0          36.0   NaN        33.0          32.0  170.0   \n",
       "1984 Summer  30.0   50.0          47.0  45.0        52.0          49.0  216.0   \n",
       "     Winter  28.0   36.0          34.0   NaN        30.0          34.0  176.0   \n",
       "1988 Summer  34.0   43.0          55.0  36.0        47.0          52.0  210.0   \n",
       "     Winter  24.0   39.0          29.0  24.0        34.0          39.0  181.0   \n",
       "1992 Summer  35.0   47.0          48.0  38.0        53.0          54.0  215.0   \n",
       "     Winter  29.0   32.0          30.0  21.0        32.0          36.0  181.0   \n",
       "1994 Winter  29.0   33.0          36.0   NaN        35.0          36.0  181.0   \n",
       "1996 Summer  39.0   53.0          48.0  46.0        49.0          50.0  215.0   \n",
       "1998 Winter  29.0   34.0          36.0  16.0        37.0          40.0  185.0   \n",
       "2000 Summer  39.0   46.0          52.0  38.0        53.0          46.0  226.0   \n",
       "2002 Winter  30.0   38.0          40.0  20.0        36.0          48.0  188.0   \n",
       "2004 Summer  43.0   50.0          48.0  43.0        55.0          52.0  226.0   \n",
       "2006 Winter  32.0   38.0          40.0  29.0        42.0          44.0  188.0   \n",
       "2008 Summer  45.0   51.0          53.0  42.0        45.0          58.0  226.0   \n",
       "2010 Winter  34.0   38.0          45.0  28.0        41.0          40.0  194.0   \n",
       "2012 Summer  41.0   49.0          56.0  39.0        49.0          54.0  221.0   \n",
       "2014 Winter  32.0   37.0          41.0  30.0        42.0          45.0  193.0   \n",
       "2016 Summer  38.0   53.0          60.0  43.0        50.0          52.0  218.0   \n",
       "\n",
       "                                                                   \n",
       "Team        France Great Britain  India Switzerland United States  \n",
       "Year Season                                                        \n",
       "1980 Summer  203.0         205.0  196.0       197.0           NaN  \n",
       "     Winter  185.0         183.0    NaN       186.0         193.0  \n",
       "1984 Summer  214.0         203.0  188.0       204.0         213.0  \n",
       "     Winter  185.0         189.0    NaN       184.0         193.0  \n",
       "1988 Summer  203.0         205.0  193.0       204.0         216.0  \n",
       "     Winter  195.0         186.0    NaN       189.0         193.0  \n",
       "1992 Summer  203.0         205.0  188.0       200.0         216.0  \n",
       "     Winter  194.0         184.0    NaN       192.0         193.0  \n",
       "1994 Winter  195.0         185.0    NaN       192.0         193.0  \n",
       "1996 Summer  201.0         205.0  191.0       200.0         216.0  \n",
       "1998 Winter  198.0         185.0  183.0       192.0         195.0  \n",
       "2000 Summer  218.0         204.0  195.0       197.0         207.0  \n",
       "2002 Winter  190.0         193.0  183.0       192.0         193.0  \n",
       "2004 Summer  202.0         207.0  195.0       197.0         213.0  \n",
       "2006 Winter  190.0         193.0  183.0       193.0         195.0  \n",
       "2008 Summer  203.0         207.0  192.0       197.0         211.0  \n",
       "2010 Winter  191.0         193.0  183.0       203.0         193.0  \n",
       "2012 Summer  208.0         211.0  196.0       198.0         216.0  \n",
       "2014 Winter  191.0         193.0  173.0       200.0         196.0  \n",
       "2016 Summer  215.0         207.0  200.0       194.0         211.0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df, \n",
    "               index=['Year', 'Season'], \n",
    "               columns='Team', \n",
    "               values=['Age', 'Height'], \n",
    "               aggfunc='max')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
